<template>

  <el-dialog title="设置" :visible.sync="dialogVisible" width="500px" :before-close="handleClose">
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item>
        <el-alert title="修改/设置分行信息!" type="warning"></el-alert>
      </el-form-item>
      <el-form-item label="所属分行" label-width="100px">
        <el-select v-model="ruleForm.bankId" clearable placeholder="请选择">
          <el-option v-for="item in branchList" :key="item.deptId" :label="item.deptName" :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>

</template>
<script>
  import {
    listDept
  } from '@/api/dept'

  import {
    userRoleUpdateApi
  } from '@/api/user'
  import {
    Debounce
  } from '@/utils/validate'
  export default {
    data() {
      return {
        ruleForm: {
          bankId: "",
          uid: []
        },
        dialogVisible: false,
        branchList: []
      };
    },
    methods: {
      show(bankId, uid) {
        console.log(uid);
        this.ruleForm.bankId = bankId ? Number(bankId) : ''
        this.ruleForm.uid = uid || 0;
        this.branchLists();
      },

      // 等级列表
      branchLists() {
        listDept({pageSize: 999}).then(async res => {
          this.branchList = res.list
          this.dialogVisible = true;
        })
      },

      handleClose(){
        this.dialogVisible = false;
        this.$emit("refresh");
      },

      submitForm: Debounce(function(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            userRoleUpdateApi(this.ruleForm).then(res => {
              this.$message.success('编辑成功');
              this.$refs[formName].resetFields()
              this.$emit("refresh");
              this.handleClose();
            })
          } else {
            return false;
          }
        });
      }),

      resetForm(formName) {
        this.$nextTick(() => {
          this.$refs[formName].resetFields();
        })
      },
    },
  };
</script>
